package com.dwb.controller;

import com.dwb.entity.SGoodsCategory;
import com.dwb.result.Result;
import com.dwb.service.SGoodsCategoryService;
import com.dwb.utils.Empty;
import com.dwb.vo.NodeVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

import static com.dwb.utils.TreeUtil.nodeDataToTree;

/**
 * @Author manke
 * @Date 2020/6/24 16:18
 * @Version 0.0.1
 */
@RestController
@Api(value = "SGoodsCategoryController", description = "服务栏目分类控制器")
@RequestMapping("/SGoodsCategory")
public class SGoodsCategoryController {
    @Autowired
    private SGoodsCategoryService sGoodsCategoryService;


    @ApiOperation(value = "delete")
    @PostMapping("/delete")
    public Result<String> delete(@RequestBody Map<String, String> parameters) {
        sGoodsCategoryService.delete(parameters);
        return Result.newSuccess();


    }

    /**
     * 获取分类分类的详情信息
     *
     * @param parameters
     * @return
     */
    @PostMapping("/getInfo")
    @ApiOperation(value = "getInfo", notes = "查看分类的详情信息")
    public Result<SGoodsCategory> getInfo(@RequestBody Map<String, String> parameters) {
        Result<SGoodsCategory> result = Result.newSuccess();
        SGoodsCategory sGoodsCategory = sGoodsCategoryService.getInfo(parameters);
        result.setDataMap(sGoodsCategory);
        return result;

    }

    /**
     * 根据pid添加或者修改服务栏目分类详情
     *
     * @param sGoodsCategory
     * @return
     */
    @PostMapping("/addOrUpdate")
    @ApiOperation(value = "addOrUpdate", notes = "添加OR修改服务栏目分类")
    public Result<String> add(@RequestBody SGoodsCategory sGoodsCategory) {
        if (Empty.INT_EMPTY(sGoodsCategory.getId())) {
            sGoodsCategoryService.insert(sGoodsCategory);
        } else {
            sGoodsCategoryService.update(sGoodsCategory);
        }

        return Result.newSuccess();

    }

    /**
     * 获取分类列表
     *
     * @return
     */
    @GetMapping("/getList")
    @ApiOperation(value = "getList", notes = "获取分类列表")
    public Result<List<NodeVo>> getList() {
        Result<List<NodeVo>> result = Result.newSuccess();
        List<SGoodsCategory> list = sGoodsCategoryService.getList();
        List<NodeVo> children = nodeDataToTree(list).get(String.valueOf(0)).getChildren();
        result.setDataMap(children);
        return result;


    }


}
